<?php
namespace app\admin\controller;
class Tiecat extends Base {
    //列表
    public function lists() {
        if (is_post()) {
            $d = $this->post;
            $map = [];
            if ($d['keyword'] != '') {
                $map[] = ['and', 'a.cat_title', 'like', "%{$d['keyword']}%"];
            }
            if ($d['cat_id'] != '') {
                $map[] = ['and', 'a.parent_id', '=', $d['cat_id']];
            }

            $w = where($map);
            $rs = $this->db->query("select a.*,b.cat_title ptitle from tie_cat a left join tie_cat b on a.parent_id = b.id $w[0] order by a.sort desc,a.id desc" . limit($d['page'], $d['limit']), $w[1], '', true);
            if ($rs) {
                foreach ($rs[0] as $key => $val) {
                    $rs[0][$key]['create_time'] = date('Y-m-d H:i', $val['create_time']);
                }
                suc($rs[0], $rs[1]);
            } else {
                suc([], 0);
            }
            $this->fetch();
        }
        $cat = m('tiecat', 'c')->allSubCat();
        return $this->fetch(['cat' => $cat]);
    }

    //添加
    public function add() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'parent_id'=>'require',
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $d['cat_tree'] = m('tiecat', 'c')->buildTree($d['parent_id']);
            $d['create_time'] = time();
            $d['level'] = count(explode(',',$d['cat_tree'])) - 3;
            $id = $this->db->insert('tie_cat', $d);
            if ($id) {
                cookie('js_msg', '添加成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('添加失败');
            }
        }
        $all = [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'depath' => 0,
            ]
        ];
        $cat = $all + m('tiecat', 'c')->allSubCat();
        return $this->fetch(['cat' => $cat]);
    }

    //编辑
    public function edit() {
        if (is_post()) {
            $d = $this->post;
            $rule = [
                'id'=>'require',
                'parent_id'=>'require',
                'cat_title'=>'require',
            ];
            $msg = [];
            check($rule,$msg,$d);
            $info = $this->db->find('tie_cat', $d['id']);
            $d['cat_tree'] = m('tiecat', 'c')->buildTree($d['parent_id']);
            $d['update_time'] = time();
            $id = $this->db->update('tie_cat', $d);
            m('tiecat','c')->editLevel($d['cat_tree']);
            if ($id) {
                $old_tree = $info['cat_tree'] . $info['id'] . ',';
                $new_tree = $d['cat_tree'] . $info['id'] . ',';
                $this->db->query("update tie_cat set cat_tree=REPLACE(cat_tree,'{$old_tree}','{$new_tree}') where cat_tree like ?", [$old_tree . '%']);
                m('tiecat','c')->editLevel($new_tree);
                cookie('js_msg', '修改成功');
                cookie('js_function', 'preload');
                return suc();
            } else {
                err('修改失败');
            }
        }
        $all = [
            '100all' => [
                'id' => 0,
                'parent_id' => 0,
                'cat_title' => '顶级分类',
                'cat_tree' => ',0,',
                'depath' => 0,
            ]
        ];
        $cat = $all + m('tiecat', 'c')->allSubCat();
        $id = $this->get['id'];
        $info = $this->db->query("select * from tie_cat where id=?", [$id], 1);
        //干掉自身及以下的分类
        $cat_tree = $info['cat_tree'] . $info['id'] . ",";
        foreach ($cat as $k => $v) {
            if (strpos($v['cat_tree'], $cat_tree) === 0 || $v['id'] == $info['id']) {
                unset($cat[$k]);
            }
        }

        return $this->fetch(['cat' => $cat, 'info' => $info]);
    }

    //删除
    public function del() {
        if (is_post()) {
            $d = $this->post;
            $rs = $this->db->query("select id from tie_cat where parent_id = ? limit 1", [$d['id']], 1);
            if ($rs)
                err('该分类下有子分类,不能被删除!');

            $res = $this->db->query("select id from article where cat_id = ? limit 1", [$d['id']], 1);
            if ($res)
                err('分类下有文章，不能删除!');

            $this->db->delete('tie_cat', $d['id']);
            cookie('js_msg', '删除成功');
            cookie('js_function', 'close|reload');
            return suc();
        }
    }

    #批量删除

    function delall() {
        if (is_post()) {
            $d = $this->post;
            $ids = $d['id'];
            foreach ($ids as $id) {
                $rs = $this->db->query("select id from tie_cat where parent_id = ? limit 1", [$id], 1);
                if ($rs)
                    err('该分类下有子分类,不能被删除!');

                $res = $this->db->query("select id from article where cat_id = ? limit 1", [$id], 1);
                if ($res)
                    err('分类下有帖子，不能删除!');

                $this->db->delete('tie_cat', $id);
            }
            cookie('js_msg', '删除成功');
            cookie('js_function', 'close|reload');
            return suc();
        }
    }

    //详情
    public function detail($id) {
        return $this->fetch();
    }

}
